#include "resolver.h"

#include <iostream>

#include "operator.h"
#include "default.h"
#include "cache.h"
Cache CACHE;

string Resolver::resolve( string f_string )
{
	int32 f_find = f_string.rfind( "(" );
	int32 f_find2 = f_string.find( ")", f_find );

	string f_value = f_string, f_show;

	while ( f_find != -1 && f_find2 != -1 )
	{
		std::cout << std::endl << "Parenthesis: " << f_string.substr( f_find, f_find2 - f_find + 1 ) << std::endl;
		f_value = Resolver::resolve( f_string.substr( f_find + 1, f_find2 - f_find - 1 ) );
		std::cout << "Closing parenthesis..." << std::endl;

		if ( f_find > 0 && Operator::get( f_string.substr( f_find - 1, 1 ) ) == 0 && f_string.substr( f_find - 1, 1 ) != "(" )
			f_string = f_string.substr( 0, f_find ) + "*" + f_value + f_string.substr( f_find2 + 1 );

		else if ( f_find > 0 && Operator::get( f_string.substr( f_find - 1, 1 ) ) == Operator::MINUS && f_find > 1 && 
			( Operator::get( f_string.substr(f_find - 2, 1) ) != 0 || f_string.substr(f_find - 2, 1) == "(" ) || f_find == 1 )
			f_string = f_string.substr( 0, f_find ) + "1*" + f_value + f_string.substr( f_find2 + 1 );

		else f_string = f_string.substr( 0, f_find ) + f_value + f_string.substr( f_find2 + 1 );

		f_find = f_string.rfind( "(" );
		f_find2 = f_string.find( ")", f_find );
	}

	bool f_exec;
	for ( int32 f_all = 0; f_all < 4; ++f_all )
	{
		f_find = 0;
		f_exec = false;

		for ( int32 f_all2 = 0; f_all2 < f_string.size(); ++f_all2 )
		{
			if ( f_all2 == 0 && Operator::get( f_string.substr( f_all2, 1 ) ) == Operator::MINUS )
			{
				if ( f_string.size() == 1 )
					break;
				++f_all2;
			}


			if ( Operator::get( f_string.substr( f_all2, 1 ) ) != 0 || f_all2 == f_string.size() - 1 )
			{
				if ( f_exec )
				{			
					if ( Operator::get( f_string.substr( f_all2, 1 ) ) != 0 )
					{
						f_value = Operator::execute( f_string.substr( f_find, f_all2 - f_find ) );
						f_string = f_string.substr( 0, f_find ) + f_value + f_string.substr( f_all2 );
					}
					else
					{
						f_value = Operator::execute( f_string.substr( f_find, f_all2 - f_find + 1 ) );
						f_string = f_string.substr( 0, f_find ) + f_value;
					}

					f_show = f_string;
					f_find2 = f_show.find( "I" );
					while( f_find2 != -1 )
					{
						for ( uint32 f_change = f_find2; f_change < f_show.size(); ++f_change )
						{
							if ( Operator::get( f_show.substr( f_change, 1 ) ) != 0 )
							{
								f_show = f_show.substr( 0, f_find2 ) +
									 CACHE.get( f_show.substr( f_find2, f_change - f_find2 ) ).get_string() +
									 f_show.substr( f_change );
								break;
							}
							if ( f_change == f_show.size() - 1 )
							{
								f_show = f_show.substr( 0, f_find2 ) + CACHE.get( f_show.substr( f_find2 ) ).get_string();
								break;
							}
						}
						f_find2 = f_show.find( "I" );
					}

					std::cout << "Current: " << f_show << std::endl;

					--f_all;
					break;
				}

				if ( Operator::get( f_string.substr( f_all2, 1 ), f_all ) != 0 )
				{
					f_exec = true;
					if ( Operator::get( f_string.substr( f_all2 + 1, 1 ) ) == Operator::MINUS )
						++f_all2;

					if ( f_all2 == f_string.size() - 1 )
						f_string += "0";
				}
				else f_find = f_all2 + 1;
			}
		}
	}

	if ( f_value.find( "I" ) == -1 )
	{
		std::cout << "Reciving I" << CACHE.size() << ": " << f_value << std::endl;
		f_string = CACHE.add_get( f_value );
	}

	//if ( f_string.substr( 0, 1 ) == "-" ) f_value = "-" + f_value;
	return f_string;
}
